<?php

namespace Topxia\Service\Kuke\Dao\Impl;

use Topxia\Service\Common\BaseDao;
use Topxia\Service\Kuke\Dao\LearningCardDao;

class LearningCardDaoImpl extends BaseDao implements LearningCardDao
{
  //数据表名
    protected $table = 'learningcard';
    //后台获取学习卡的批次信息
    public function getBatch()
    {
      $sql = "SELECT distinct batch FROM {$this->table} ";
      // var_dump($sql);die;
      return $this->getConnection()->fetchAll($sql);
    }
    //获取学习卡信息
    public function get($start, $limit,$sbatch)
    {
		
        $sql = "SELECT * from {$this->table} where 1=1 ";
		if(!empty($sbatch['cardId'])){
			
			$sql .= ' and cardId = \''.$sbatch['cardId'].'\'';
			
		}elseif(!empty($sbatch['batch'])){
			
			$sql .= ' and batch = \''.$sbatch['batch'].'\'';
			
		}	
		
		$sql.="LIMIT {$start},{$limit}";
		
        // var_dump($sql);die;
        return $this->getConnection()->fetchAll($sql);
    } 
	
	public function getcm($cardid)
    {
        $sql = "SELECT * from {$this->table} where Id = '$cardid'   ";
        // var_dump($sql);die;
        return $this->getConnection()->fetchAssoc($sql);
    }

    //获取学习卡总量
    public function total()
    {   
        $sql= "SELECT count('id') from {$this->table} ";
        return $this->getConnection()->fetchAll($sql);
    }
    public function total1($conditions)
    {   
        $sql = " select count(id) from {$this->table} as num where 1=1";
		
        if(!empty($conditions['cardId'])){
          $sql .= ' and cardId = \''.$conditions['cardId'].'\'';
        }elseif(!empty($conditions['batch'])){
          $sql .=' and batch= \''.$conditions['batch'].'\'';
        }
        return $this->getConnection()->fetchAll($sql);
    }
	//获取总数
	public function getCardCount($conditions)
	{
		$sql = " select count(id) from {$this->table} as num where userId={$conditions}";

//		if(isset($conditions['promo'])){
//			$sql .= ' and promo = \''.$conditions['promo'].'\'';
//		}
//		if(isset($conditions['pc'])){
//			$sql .= ' and pc = \''.$conditions['pc'].'\'';
//		}	
		
		return  $this->getConnection()->fetchColumn($sql);
	}

    //添加学习卡
   public function buildding($conditions)
   {
        $sql =  ' select card('.$conditions['num'].', '.$conditions['usetime'].',\''.$conditions['money'].'\',\''.$conditions['ctime'].'\',\''.$conditions['category'].'\',\''.$conditions['batch'].'\',\''.$conditions['cateId'].'\',\''.$conditions['valid'].'\',\''.$conditions['type'].'\',\''.$conditions['title'].'\');';

        return  $this->getConnection()->fetchColumn($sql);

   }

   //删除学习卡

   public function delete($id)
   {
       
        return $this->getConnection()->exec("delete  from {$this->table} where id = ".$id);
   }

   //更新学习卡状态
   public function updateStatus($status,$id,$userId,$rechargetime)
   {
        $sql = "update {$this->table} set status=".$status.",userId=".$userId.",rechargetime=".$rechargetime." where id=".$id;
        return $this->getConnection()->exec($sql);
   }

   //学习卡批次的查询
   public function  search($conditions,$start, $limit)
   {
        $sql = "SELECT distinct batch from {$this->table} where 1=1 ";
      
         if(!empty($conditions['batch'])){
          $sql .=' and batch= \''.$conditions['batch'].'\'';
        }
          $sql .=" limit {$start},{$limit}";
       // var_dump($sql);die;

        return $this->getConnection()->fetchAll($sql);

   }


      //学习卡卡号的查询
   public function  searchId($conditions,$start, $limit)
   {
        $sql = "SELECT * from {$this->table} where 1=1 ";
      
          if(!empty($conditions['cardId'])){
			  $sql .= ' and cardId = \''.$conditions['cardId'].'\'';
		  }elseif(!empty($conditions['batch'])){
			  $sql .=' and batch= \''.$conditions['batch'].'\'';
		  }
          $sql .=" limit {$start},{$limit}";
       // var_dump($sql);die;

        return $this->getConnection()->fetchAll($sql);

   }

   //学习卡批次的查询
   public function getMaxBatch()
   {
      $sql = "select MAX(batch) from {$this->table}";
      return $this->getConnection()->fetchAll($sql);
   }

   //前台对学习卡状态的查询
   public function select($cardId)
   {
       $sql = "SELECT * from {$this->table} where 1=1 ";
       if(!empty($cardId)){
          $sql .=' and cardId = \''.$cardId.'\'';
        }

      return $this->getConnection()->fetchAll($sql);
   }

   //通过用户的id获取该用户所拥有的学习卡
   public function getCard($uid,$start,$limit)
   {
	   
      $sql = "SELECT * FROM {$this->table} where userId =".$uid;
	   $sql .= " order by id desc, id desc limit $start,$limit";
      return $this->getConnection()->fetchAll($sql) ? : null;
   } 
	//API调用学习卡列表
	public function getmeCard($userId)
   {
	   
      $sql = "SELECT * FROM {$this->table} where userId =".$userId;
      return $this->getConnection()->fetchAll($sql);
   }


   //订单页面获取相应的学习卡
   public function getOrderCard($data)
   {
    // var_dump($data);exit;
      $sql = "SELECT * FROM {$this->table} where userId=".$data['uid']." and status = '1'   and (cateId=".$data['cateId']." or cateId=0 ) and (category in({$data['category']}) or category=0) ";
      return $this->getConnection()->fetchAll($sql);
   }

   //订单页面获取相应的classroom学习卡
   public function getRoomCard($info)
   {
      $sql = "SELECT * FROM {$this->table} where userId=".$info['uid']." and status='1'  and (category in({$info['category']}) or category='0' ) ";
      return $this->getConnection()->fetchAll($sql);

   }

   //订单页面获取已经选定的学习卡的信息
   public function selectId($id)
   {
      $sql = "select * from {$this->table} where id=".$id;
      return $this->getConnection()->fetchAssoc($sql);
   }
   //提交订单之后更改学习卡的面额
   public function updateMoney($id,$diff)
   {
      $sql = "update {$this->table} set money=".$diff." where id=".$id;
      return $this->getConnection()->exec($sql);
   }

   //当学习卡的面额使用完毕时更改学习卡的状态
    public function upStatus($status,$id)
   {
      $sql = "update {$this->table} set status='".$status."' where id=".$id;
      return $this->getConnection()->exec($sql);
   }

   //按批次导出学习卡
   public function selectBatch($batch,$limit)
   {
      $sql= "select * from {$this->table} where batch=".$batch.' limit '.($limit*1000).',1000';
      return $this->getConnection()->fetchAll($sql) ? : null;
   }

   //导出学习卡的字段名
   public function selectFields()
   {
      $sql = "desc {$this->table}";
      return $this->getConnection()->fetchAll($sql);
   }


   //获得已有的学习卡的总量
   public function getNum()
   {
      $sql = "select count(id) from {$this->table}";
      return $this->getConnection()->fetchAll($sql);
   }

   //获得已经充值的学习卡的数量
   public function getCnum($nbatch)
   {
        $sql = "select count(id) from {$this->table} where userId!='0' ";
         if(!empty($nbatch)){
          $sql .=' and batch in ('.$nbatch.')';
        }

        return $this->getConnection()->fetchAll($sql);


   }

   public function getNoNum($nbatch)
   {
      $sql = "select count(id) from {$this->table} where userId = '0'";
       if(!empty($nbatch)){
          $sql .=' and batch in ('.$nbatch.')';
        }
        return $this->getConnection()->fetchAll($sql);
   }
   public function getInfo()
   {
      $sql = "select money,category,batch,deno,title from {$this->table} group by batch ";
      return $this->getConnection()->fetchAll($sql);
   }
   //获得学习卡一共有多少学习卡已充值
   public function getCzNum()
   {
      $sql = "select count(id) from {$this->table} where userId!= '0'";
      return $this->getConnection()->fetchAll($sql);
   }

}